import React from 'react';
import { Form, Input, Button, Checkbox, message } from 'antd';
import { useNavigate } from 'react-router-dom';
import axios from '../utils/request';
import './Login.css';

const Login = () => {
  const navigate = useNavigate();

  const onFinish = async (values) => {
    try {
      const res = await axios.post('/users/login', {
        username: values.username,
        password: values.password
      });
      if (res.data && res.data.accessToken) {
        localStorage.setItem('accessToken', res.data.accessToken);
        navigate('/dashboard');
      } else {
        message.error('账号或密码错误');
      }
    } catch (e) {
      message.error('登录请求失败');
    }
  };

  return (
    <div className="login-bg">
      <div className="login-blur" />
      <div className="login-container">
        <img src={process.env.PUBLIC_URL + '/logo192.png'} alt="logo" style={{ display: 'block', margin: '0 auto 18px auto', width: 64, height: 64, borderRadius: 16, boxShadow: '0 2px 16px rgba(0,114,255,0.10)' }} />
        <div className="login-title">智慧园区管理平台</div>
        <div className="login-subtitle">请登录以进入系统</div>
        <Form
          name="login"
          onFinish={onFinish}
          layout="vertical"
        >
          <Form.Item
            name="username"
            label="账号"
            rules={[
              { required: true, message: '请输入账号' },
              { min: 6, max: 9, message: '账号长度为6-9位' },
            ]}
          >
            <Input placeholder="请输入账号" />
          </Form.Item>
          <Form.Item
            name="password"
            label="密码"
            rules={[
              { required: true, message: '请输入密码' },
              { min: 7, max: 12, message: '密码长度为7-12位' },
            ]}
          >
            <Input.Password placeholder="请输入密码" />
          </Form.Item>
          <Form.Item>
            <Checkbox style={{ color: '#fff' }}>记住密码</Checkbox>
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit" block className="login-btn">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
};

export default Login; 